﻿using System;
using System.Collections.Generic;
using HiTeachers.Data.Manager;
using HiTeachers.Entities;
using HiTeachers.Interfaces;
using HiTeachers.Utilities;
using NHibernate;

namespace HiTeachers.Data
{
    public class NewDao : AbstractNHibernateDao<New, Guid>, INewDao
    {
        public long TotalNumberOfNew()
        {
            const string hsql = "select count(*) from New n";

            IQuery query = NHibernateSession.CreateQuery(hsql);

            return query.UniqueResult<long>();
        }

        public IList<New> GetRelationNews(Guid itId, DateTime aroundDate, int maxItem)
        {
            try
            {
                const string hsql = "from New n where n.ID != :itemId";

                IQuery query = NHibernateSession.CreateQuery(hsql);

                query.SetGuid("itemId", itId);

                query.SetMaxResults(maxItem);
                query.SetFirstResult(0);

                return query.List<New>();
            }
            catch (Exception ex)
            {
                Logger.Debug(ex.Message);
                return new List<New>();
            }
        }

        public IList<New> GetNews(int maximumRows, int startRowIndex)
        {
            try
            {
                const string hsql = "from New n order by n.CreatedDate desc";

                IQuery query = NHibernateSession.CreateQuery(hsql);

                query.SetMaxResults(maximumRows);
                query.SetFirstResult(startRowIndex);

                return query.List<New>();
            }
            catch (Exception ex)
            {
                Logger.Debug(ex.Message);
                return new List<New>();
            }

        }
    }
}
